$(function () {
	vm.getColModelList()
//    $("#jqGrid").Grid({
//        url: '../sale/car/list',
//        colModel: [
//			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
//			{label: '销售顾问', name: 'productCarId', index: 'product_car_id', width: 80},
//			{label: '客户', name: 'productCarId', index: 'product_car_id', width: 80},
//			{label: '客户手机号', name: 'productCarId', index: 'product_car_id', width: 80},
//			{label: '车性名称', name: 'productCarId', index: 'product_car_id', width: 80},
//			{label: 'vin', name: 'saleBookId', index: 'sale_book_id', width: 80},
//			{label: '交车日期', name: 'plateCost', index: 'plateCost', width: 80},
//			{label: '整车利润', name: 'plateCost', index: 'plateCost', width: 80},
//			{label: '单车利润', name: 'plateCost', index: 'plateCost', width: 80},
//			{label: '精品利润', name: 'plateCost', index: 'plateCost', width: 80},
//			{label: '衍生利润', name: 'plateCost', index: 'plateCost', width: 80},
//			{label: '厂家返利', name: 'plateCost', index: 'plateCost', width: 80},
//			{label: '上牌费用', name: 'plateCost', index: 'plate_cost', width: 80},
//			{label: '按揭费用', name: 'acquireCost', index: 'acquire_cost', width: 80},
//			{label: '保险费用', name: 'insuranceCost', index: 'insurance_cost', width: 80}]
//    });
});

var vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        showDiscount: false,
        showRoyalty: false,
        title: null,
		saleCar: {},
		productDiscount: {},
		productRoyalty: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		getColModelList:function(){
			$("#jqGrid").Grid({
                url: '../sale/car/list',
                colModel: [{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
                           {label: '车型', name: 'productCarId', index: 'product_car_id', hidden: true},
       	      			{label: '销售顾问', name: 'saleName', index: 'sale_book_id', width: 120},
       	    			{label: '客户', name: 'customerName', index: 'sale_book_id', width: 120},
       	    			{label: '客户手机号', name: 'customerName', index: 'sale_book_id', width: 120},
       	    			{label: '车系', name: 'carSerie', index: 'product_car_id', width: 120},
       	    			{label: '车型', name: 'carModel', index: 'product_car_id', width: 120},
       	    			{label: '配置', name: 'selection', index: 'product_car_id', width: 120},
       	    			{label: 'vin', name: 'vin', index: 'vin', width: 120},
       	    			{label: '交车日期', name: 'overDate', index: 'over_date', width: 120},
    	    			{label: '保险公司', name: 'financecompanyName', index: 'financecompanyName', width: 200},
    	    			{label: '交强险', name: 'trafficCost', index: 'trafficCost', width: 200},
    	    			{label: '商业险', name: 'insuranceCost', index: 'insuranceCost', width: 200},
    	    			{label: '保险返点', name: 'financeProfit', index: 'financeProfit', width: 200},
    	    			{label: '毛利', name: 'financePoundage', index: 'financePoundage', width: 200}]
            });
//			Ajax.request({
//                url: "../base/allProfits/queryAll",
//                async: true,
//                successCallback: function (r) {
//                	var colModelList=[{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
//    	      			{label: '销售顾问', name: 'productCarId', index: 'product_car_id', width: 200},
//    	    			{label: '客户', name: 'productCarId', index: 'product_car_id', width: 200},
//    	    			{label: '客户手机号', name: 'productCarId', index: 'product_car_id', width: 200},
//    	    			{label: '车性名称', name: 'productCarId', index: 'product_car_id', width: 200},
//    	    			{label: 'vin', name: 'saleBookId', index: 'sale_book_id', width: 200},
//    	    			{label: '交车日期', name: 'plateCost', index: 'plateCost', width: 200},
//    	    			{label: '整车利润', name: 'plateCost', index: 'plateCost', width: 200},
//    	    			{label: '单车利润', name: 'plateCost', index: 'plateCost', width: 200},
//    	    			{label: '精品利润', name: 'plateCost', index: 'plateCost', width: 200},
//    	    			{label: '衍生利润', name: 'plateCost', index: 'plateCost', width: 200},
//    	    			{label: '厂家返利', name: 'plateCost', index: 'plateCost', width: 80},
//    	    			{label: '上牌费用', name: 'plateCost', index: 'plateCost', width: 80},
//    	    			{label: '按揭费用', name: 'acquireCost', index: 'acquire_cost', width: 80},
//    	    			{label: '保险费用', name: 'insuranceCost', index: 'insurance_cost', width: 80}];
//                	if(r.list&&r.list.length>0){
//                		for (var i = 0; i < r.list.length; i++) {
//                			colModelList.push({label:r.list[i].name,name:r.list[i].mapName,index:r.list[i].mapName,width:80});
//						}
//                	}
//                }
//            });
		},
		query: function () {
			vm.reload();
		},
		updateDiscount:function(){
			vm.showList = false;
			vm.showDiscount=true;
			vm.showRoyalty=false;
		},
		updateRoyalty:function(){
			vm.showList = false;
			vm.showDiscount=false;
			vm.showRoyalty=true;
		},
		update: function (event) {
            var id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
		},
		handleDiscountSubmit:function(){
			
		},
		handleRoyaltySubmit:function(){
			
		},
		saveOrUpdate: function (event) {
            var url = vm.saleCar.id == null ? "../sale/car/save" : "../sale/car/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.saleCar),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            var ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../salecar/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../salecar/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.saleCar = r.saleCar;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.showDiscount=false;
			vm.showRoyalty=false;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: 1
            }).trigger("reloadGrid");
//            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});